<template>
  <div class="common-layout">
    <el-container class="main-container">
      <el-header class="common-header">
        <p>墨韵书坊</p>
        <el-input v-model="input" class="header-input" placeholder="Please input">
          <template #append>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
        <div class="header-box0">
          <div class="header-box" v-if="!isLogin">
            <RouterLink to="/login">
              <el-text class="mx-1" size="large">登陆</el-text>
              <el-text class="mx-1" size="large">|</el-text>
              <el-text class="mx-2" size="large">注册</el-text>
            </RouterLink>
          </div>
          <div class="header-box" v-if="isLogin">
            <img src="@/assets/img/avart.png" alt="">
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px" class="common-aside">
          <h3 style="text-align: center;">全部书籍分类</h3>
          <!-- 写一些关于书籍的分类 -->
          <IndexBookClassify />
        </el-aside>
        <el-main class="common-main">
          <!-- 轮播图效果实现 -->
          <el-carousel height="300px">
            <el-carousel-item v-for="item in imageList" :key="item">
              <img :src="item" alt="轮播图" style="height: 300px; width: 100%; object-fit: cover;">
            </el-carousel-item>
          </el-carousel>
          <div class="common-news">
            <div class="common-news-box">
              <img src="@/assets/img/xuanchuan01.jpg" alt="">
            </div>
            <div class="common-news-box">
              <img src="@/assets/img/xuanchuan02.jpg" alt="">
            </div>
            <div class="common-news-box">
              <img src="@/assets/img/xuanchuan03.jpg" alt="">
            </div>
            <div class="common-news-box">
              <img src="@/assets/img/xuanchuan04.jpg" alt="">
            </div>
          </div>
        </el-main>
      </el-container>

      <div class="book-recommend">
        <div class="book-recommend-header">热门小说推荐</div>
        <div class="book-list">
          <div class="book-card" v-for="(novel, index) in novelList" :key="index" @click="handleNovelClick(novel)">
            <div class="book-image">
              <img :src="novel.image" alt="小说封面">
              <div class="book-tag" v-if="novel.isFree">免费阅读</div>
            </div>
            <div class="book-info">
              <h4 class="book-title">{{ novel.title }}</h4>
              <div class="book-author">{{ novel.author }}</div>
              <div class="book-footer">
                <el-rate v-model="novel.rating" disabled :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                  class="book-rating" />
                <el-button type="success" size="small" class="read-btn">立即阅读</el-button>
              </div>
            </div>
          </div>
        </div>
        <div class="view-more">
          <el-button type="text">查看更多 ></el-button>
        </div>
        <div class="book-recommend-header">热门书籍推荐</div>
        <div class="book-list">
          <div class="book-card" v-for="(book, index) in bookList" :key="index">
            <div class="book-image">
              <img :src="book.image" alt="书籍封面">
              <div class="book-tag" v-if="book.discount">限时折扣</div>
            </div>
            <div class="book-info">
              <h4 class="book-title">{{ book.title }}</h4>
              <div class="book-author">{{ book.author }}</div>
              <div class="book-price">
                <span class="current-price">¥{{ book.price.current }}</span>
                <span class="original-price" v-if="book.price.original">¥{{ book.price.original }}</span>
              </div>
              <div class="book-footer">
                <el-rate v-model="book.rating" disabled :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                  class="book-rating" />
                <RouterLink :to="`/buy-${book.id}`" target="_blank">
                  <el-button type="primary" size="small" class="buy-btn">立即购买</el-button>
                </RouterLink>
              </div>
            </div>
          </div>
        </div>
        <div class="view-more">
          <el-button type="text">查看更多 ></el-button>
        </div>
      </div>
      <el-footer class="common-footer">
        <p>© 2025 墨韵书坊 版权所有</p>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
import IndexBookClassify from '@/components/IndexBookClassify.vue'
import book1 from '@/assets/img/book1.jpg'
import santi from '@/assets/img/santi.avif'
import kuangren from '@/assets/img/kuangren.jpg'
import huozhe from '@/assets/img/huozhe.jpeg'
import pingfan from '@/assets/img/pingfan.jpg'
import weicheng from '@/assets/img/weicheng.jpg'
import sanguo from '@/assets/img/sanguo.jpg'
import shuihu from '@/assets/img/shuihu.jpg'

export default {
  components: {
    IndexBookClassify
  },
  data() {
    return {
      imageList: [],
      isLogin: false,
      input: '',
      novelList: [
        {
          title: '三体全集',
          author: '刘慈欣',
          image: santi,
          rating: 4.8,
          isFree: true,
          readLink: '/book01'
        },
        {
          title: '平凡的世界',
          author: '路遥',
          image: pingfan,
          rating: 4.7,
          isFree: false,
          readLink: '/book02'
        },
        {
          title: '狂人日记',
          author: '鲁迅',
          image: kuangren,
          price: { current: 29.90, original: 39.00 },
          rating: 4.8,
          discount: true,
          readLink: '/book03'
        },
        {
          title: '活着',
          author: '余华',
          image: huozhe,
          price: { current: 35.00 },
          rating: 4.9,
          discount: false,
          readLink: '/book04'
        },
        {
          title: '围城',
          author: '钱钟书',
          image: weicheng,
          price: { current: 42.00, original: 50.00 },
          rating: 4.7,
          discount: false,
          readLink: '/book05'
        }
      ],
      bookList: [
        {
          id: 'book01',
          title: '三国演义',
          author: '罗贯中',
          image: sanguo,
          price: { current: 89.90, original: 99.00 },
          rating: 4.5,
          discount: true
        },
        {
          id: 'book02',
          title: '水浒传',
          author: '施耐庵',
          image: shuihu,
          price: { current: 79.00 },
          rating: 5,
          discount: false
        }
      ]
    };
  },
  async mounted() {
    this.imageList = await this.loadImages();
    this.checkLogin();
  },
  methods: {
    async loadImages() {
      const images = [
        import('@/assets/img/index-img1.jpg'),
        import('@/assets/img/index-img2.jpg'),
        import('@/assets/img/index-img3.jpg')
      ];
      const loadedImages = await Promise.all(images);
      return loadedImages.map((img) => img.default);
    },
    handleNovelClick(novel) {
      console.log('进入小说阅读', novel);
      this.$router.push(novel.readLink);
    },
    checkLogin() {
      if (localStorage.getItem('token')) {
        this.isLogin = true;
      }
    },
    handleSearch() {
      const query = this.input.trim();
      if (!query) return;
      window.open(`https://www.baidu.com/s?wd=${encodeURIComponent(query)}`, '_blank');
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
  background-color: #f8f9fa;
}

.common-layout {
  background: #f8f9fa;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 主容器 */
.main-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* 顶部导航栏 */
.common-header {
  height: 80px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  padding: 0 60px;
}

.common-header p {
  font-size: 26px;
  font-family: "SimSun", "宋体", sans-serif;
  font-weight: bold;
  margin-right: 100px;
  color: #333;
}

.header-input {
  width: 600px;
  margin-right: auto;
}

.header-box0 {
  display: inline-block;
  width: 150px;
  height: 50px;
}

.header-box img {
  width: 50px;
  height: 50px;
  margin-left: 50px;
  border-radius: 50%;
}

.header-box .el-text {
  cursor: pointer;
  transition: all 0.3s ease;
  color: #333;
}

.header-box .el-text:hover {
  color: rgb(151, 205, 255);
}

.mx-1,
.mx-2 {
  cursor: pointer;
  letter-spacing: 4px;
  color: #666;
}

/* 侧边栏 */
.common-aside {
  background: #fff;
  height: calc(100vh - 210px);
  margin-left: 14px;
  box-shadow: 2px 0 12px 0 rgba(0, 0, 0, 0.05);
  padding: 20px 0;
}

.common-aside h3 {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
}

/* 主内容区 */
.common-main {
  background: #fff;
  height: calc(100vh - 210px);
  padding: 20px;
  margin-left: 20px;
  box-shadow: 2px 0 12px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

/* 轮播图 */
.el-carousel {
  margin-bottom: 20px;
}

/* 新闻 */
.common-news {
  height: 150px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 30px;
}

.common-news-box {
  width: calc(25% - 10px);
  height: 130px;
  background-color: #f1f2f6;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
}

.common-news-box:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: translateY(-3px);
}

.common-news-box img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 书籍推荐 */
.book-recommend {
  background: #fff;
  padding: 20px 0;
  margin-top: 20px;
  box-shadow: 0 0 12px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.book-recommend-header {
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-bottom: 30px;
  position: relative;
  padding-bottom: 10px;
}

.book-recommend-header::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(to right, #409EFF, #67C23A);
}

.book-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.book-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
}

.book-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.12);
}

.book-image {
  position: relative;
  height: 280px;
  overflow: hidden;
}

.book-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.book-card:hover .book-image img {
  transform: scale(1.1);
}

.book-tag {
  position: absolute;
  top: 10px;
  right: -30px;
  background: linear-gradient(to right, #FF6B6B, #FF8E8E);
  color: white;
  padding: 4px 30px;
  transform: rotate(45deg);
  font-size: 12px;
  font-weight: bold;
}

.book-info {
  padding: 20px;
}

.book-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.book-author {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}

.book-price {
  display: flex;
  align-items: baseline;
  margin-bottom: 15px;
}

.current-price {
  font-size: 18px;
  color: #FF6B6B;
  margin-right: 8px;
  font-weight: bold;
}

.original-price {
  font-size: 12px;
  color: #C0C4CC;
  text-decoration: line-through;
}

.book-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.book-rating {
  flex: 1;
}

.read-btn {
  background: linear-gradient(to right, #67C23A, #85ce61);
  border-color: #67C23A;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s ease;
}

.read-btn:hover {
  background: #85ce61;
  border-color: #85ce61;
}

.buy-btn {
  padding: 8px 16px;
  border-radius: 20px;
}

.view-more {
  text-align: center;
  margin: 20px 0;
}

.view-more .el-button {
  color: #409EFF;
  font-size: 14px;
  transition: color 0.3s ease;
}

.view-more .el-button:hover {
  color: #66b1ff;
}

/* 页脚 */
.common-footer {
  background: linear-gradient(to right, #409EFF, #67C23A);
  color: white;
  text-align: center;
  padding: 10px 0;
  margin-top: auto;
}

.common-footer p {
  margin: 0;
  font-size: 14px;
}
</style>